<template>
    <div class="article_add">
        <h1>新增文章</h1>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="文章标题" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="推荐" prop="recommend">
                <el-radio-group v-model="radio">
                    <el-radio :label="1">5星</el-radio>
                    <el-radio :label="2">4星</el-radio>
                    <el-radio :label="3">3星</el-radio>
                    <el-radio :label="4">2星</el-radio>
                    <el-radio :label="5">1星</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="简介" prop="brief">
                <el-input v-model="ruleForm.brief"></el-input>
            </el-form-item>
           <el-form-item label="展示方式" prop="show">
               <el-radio-group v-model="radio_pto">
                    <el-radio :label="1">单图</el-radio>
                    <el-radio :label="2">三张图</el-radio>
                </el-radio-group>
                <span>展示类型选定后，只能替换图片</span>
                <el-upload
                  class="upload-demo"
                  action=""
                  :on-change="handleChange"
                  :file-list="fileList">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，每次只能上传一张,最大不超过5mb</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="职位描述" prop="desc">
                <el-input type="textarea" v-model="ruleForm.desc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import config from '@/assets/js/config.js'
  export default {
    data() {
      return {
        title:"",
        brief:"",
        stars:"",
        show_type_id:"",
        format_content: "",
        autoUp:false,
        ruleForm: {},
        radio: 3,
        radio_pto: 1,
        imgUrl:"",
        rules: {
          title: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          show: [
            { required: false, message: '请选择图片', trigger: 'blur' }
          ],
          Introduction: [
            { required: true, message: '请选择简介', trigger: 'blur' }
          ],
          desc: [
            { required: true, message: '请填写描述', trigger: 'blur' }
          ]
        },
        fileList: []
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
            this.$submit.post(config.newsAdd,{
              brief:this.ruleForm.brief,
              title:this.ruleForm.title,
              stars:this.radio,
              show_type_id:this.radio_pto,
              format_content:this.ruleForm.desc,
              cover_img_url1:this.imgUrl,
              cover_img_url2:"",
              cover_img_url3:""
            }).then((res) => {
              console.log("新增成功",res)
              
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleChange(file, fileList) {
        console.log(666,file)
        // this.fileList = fileList.slice(-3);
        var formData = new FormData();
				formData.append("files",file.raw);
        this.$submit.post(config.upImg, formData).then((res) => {
              console.log("图片",res)
              this.imgUrl = res.data.data.img_url;
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
    .article_add{
        padding-top: 20px;
        padding-left: 20px;
        padding-right: 20%;
        h1{
            margin-bottom: 20px;
        }
        span{
            display: inline-block;
            margin-left: 40px;
        }
        input{
            width: 70%!important;
        }
    }
</style>
